package club.xyes.zkh.ecommerce.web.api.controller;

import club.xyes.zkh.ecommerce.commons.entity.Shop;
import club.xyes.zkh.ecommerce.commons.entity.ShopImage;
import club.xyes.zkh.ecommerce.commons.properties.WebsiteConfigurationProperties;
import club.xyes.zkh.ecommerce.commons.util.EntityImagePathUtils;
import club.xyes.zkh.ecommerce.commons.vo.GeneralCollectionResult;
import club.xyes.zkh.ecommerce.commons.vo.GeneralResult;
import club.xyes.zkh.ecommerce.service.ShopImageService;
import club.xyes.zkh.ecommerce.service.ShopService;
import club.xyes.zkh.ecommerce.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * Create by 郭文梁 2019/4/17 0017 09:33
 * ShopImageController
 * 商铺图片相关数据访问控制器
 *
 * @author 郭文梁
 * @data 2019/4/17 0017
 */
@RestController
@RequestMapping("/api/shop-image")
@Api("商铺图片相关接口")
public class ShopImageController extends BaseController<ShopImage> {
    private final ShopImageService service;
    private final ShopService shopService;
    private final WebsiteConfigurationProperties websiteConfigurationProperties;

    @Autowired
    public ShopImageController(ShopImageService service,
                               ShopService shopService,
                               WebsiteConfigurationProperties websiteConfigurationProperties) {
        super(service);
        this.service = service;
        this.shopService = shopService;
        this.websiteConfigurationProperties = websiteConfigurationProperties;
    }

    /**
     * 上传新的
     *
     * @param shopId    商铺ID
     * @param imageFile 图片文件
     * @return ShopImage
     * @throws IOException IOE
     */
    @PostMapping("/_upload-image")
    @ApiOperation("上传新的商铺图片")
    public GeneralResult<ShopImage> updateImage(@RequestParam("shopId") Integer shopId, MultipartFile imageFile) throws IOException {
        if (imageFile == null || imageFile.isEmpty()) {
            return GeneralResult.badRequest("ImageFile could not be null!");
        }
        Shop shop = shopService.getRequired(shopId);
        ShopImage res = service.newImage(shop, imageFile);
        EntityImagePathUtils.prettyShopImagePath(res, websiteConfigurationProperties.getStaticServerPrefix(),
                websiteConfigurationProperties.getShopImagePath());
        return GeneralResult.ok(res);
    }

    /**
     * 通过商铺ID查找商铺图片
     *
     * @param shopId 商铺ID
     * @return ShopImage list
     */
    @GetMapping("/find-by-shop-id")
    @ApiOperation("通过店铺ID查找店铺图片")
    public GeneralCollectionResult<ShopImage> findByShopId(@RequestParam("shopId") Integer shopId) {
        List<ShopImage> images = service.findByShopId(shopId);
        images = EntityImagePathUtils.prettyShopImagePath(images, websiteConfigurationProperties.getStaticServerPrefix(),
                websiteConfigurationProperties.getShopImagePath());
        return GeneralCollectionResult.ok(images);
    }

    /**
     * 删除商铺图片
     *
     * @param id 图片ID
     * @return GR
     */
    @ApiOperation("删除商铺图片")
    @DeleteMapping("/{id}")
    public GeneralResult<ShopImage> delete(@PathVariable("id") Integer id) {
        ShopImage image = service.getRequired(id);
        //TODO 检查操作权限
        //暂未检查操作权限
        if (service.deleteById(image.getId())) {
            EntityImagePathUtils.prettyShopImagePath(image, websiteConfigurationProperties.getStaticServerPrefix(),
                    websiteConfigurationProperties.getShopImagePath());
            return GeneralResult.ok(image);
        } else {
            return GeneralResult.error("Unable to delete ShopImage for id " + id);
        }
    }

    /**
     * 重新上传轮播图片
     *
     * @param id        图片ID
     * @param imageFile 图片文件
     * @return GR
     * @throws IOException IO异常
     */
    @ApiOperation("重新上传轮播图片")
    @PostMapping("/{id}/replace")
    public GeneralResult<ShopImage> replace(@PathVariable("id") Integer id,
                                            MultipartFile imageFile) throws IOException {
        if (imageFile == null || imageFile.isEmpty()) {
            return GeneralResult.badRequest("未上传文件");
        }
        ShopImage image = service.getRequired(id);
        Shop shop = currentShop();
        if (!Objects.equals(image.getShopId(), shop.getId())) {
            return GeneralResult.permissionDenied("您无权操作该数据");
        }
        image = service.replace(image, imageFile);
        return GeneralResult.ok(image);
    }

    /**
     * 删除当前商铺的轮播图片
     *
     * @return GR
     */
    @DeleteMapping("/shop/clean")
    @ApiOperation("删除当前商铺的轮播图片")
    public GeneralResult<?> cleanForCurrentShop() {
        Shop shop = currentShop();
        int deleted = service.deleteByShopId(shop.getId());
        return GeneralResult.ok(deleted);
    }

    /**
     * 获取当前用户拥有的商铺
     *
     * @return 商铺对象
     */
    private Shop currentShop() {
        return currentShop(shopService);
    }
}
